from flask import Flask, render_template, request
# from flask_wtf import Form
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, PasswordField
from wtforms.validators import DataRequired, EqualTo
from flask import flash

app = Flask(__name__)
# 秘钥
app.config["SECRET_KEY"] = "123abchello"


class LoginForm(FlaskForm):
    # 用户名
    username = StringField(label=u"用户名:", validators=[DataRequired()])
    # 密码
    password = PasswordField(label=u"密码:", validators=[DataRequired()])
    # 确认密码
    repwd = PasswordField(label=u"确认密码:", validators=[DataRequired(), EqualTo("password", "两次密码输入不一致!")])
    # 提交
    submit = SubmitField(label=u"提交")

@app.route("/index")
def index():
    post_form = LoginForm()
    return render_template("index.html", form=post_form)

@app.route("/login", methods=["POST", "GET"])
def login():
    if request.method == "POST":
        post_form = LoginForm()
        if post_form.validate_on_submit():
            name = post_form.username.data
            password = post_form.password.data
            repwd = post_form.repwd.data
            print("姓名:", name)
            print("密码:", password)
            print("确认密码:", repwd)
            flash("欢迎您,%s" % name)
        else:
            flash("信息有误,请重新输入!!!")
        return render_template("hello.html", form=post_form)


if __name__ == '__main__':
    app.run(debug=True)
